package com.twotiger.and.service;

import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

import com.twotiger.and.Dbutil;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.bean.Loctionbean;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.Pointsdistance;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.Tools;

public class LoctionService extends Service implements LocationListener {
    private static LocationManager locationManager;
    private String currentProvider;
    private Dbutil dbutil;
    private double accuracy = 100;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(true);
        criteria.setBearingRequired(false);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setSpeedRequired(false);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        currentProvider = locationManager.getBestProvider(criteria, true);
        if(currentProvider == null){
            return;
        }
        locationManager.requestLocationUpdates(currentProvider, 1000 * 60 * 10, 0,
            LoctionService.this);
        dbutil = new Dbutil(LoctionService.this);
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    private void conmitloction(List<Loctionbean> locbean, String imei) {
    }

    @Override
    public void onLocationChanged(Location location) {

        String Longitude = PreferencesUtils.getString(this, "Longitude", "0");
        String Latitude = PreferencesUtils.getString(this, "Latitude", "0");
        double point2m = Pointsdistance.point2m(location.getLatitude(), location.getLongitude(),
            Double.valueOf(Latitude), Double.valueOf(Longitude));
        if (point2m > accuracy) {
            PreferencesUtils.putString(this, "Longitude", String.valueOf(location.getLongitude()));
            PreferencesUtils.putString(this, "Latitude", String.valueOf(location.getLatitude()));
            Loctionbean loctionbean = new Loctionbean();
            String imei = Tools.getDeviceId(this);
            loctionbean.setAltitude(String.valueOf(location.getAltitude()));
            loctionbean.setLongitude(String.valueOf(location.getLongitude()));
            loctionbean.setCtime(String.valueOf(DateUtil.getSystemTimeForLong()));
            loctionbean.setLatitude(String.valueOf(location.getLatitude()));
            if (TwoTigerApp.getInstance().getUser() != null) {
                loctionbean.setUserName(TwoTigerApp.getInstance().getUser().userName);
            } else {
                loctionbean.setUserName("");
            }
            dbutil.AddAll(loctionbean);
            List<Loctionbean> tmp = dbutil.findAll();
            conmitloction(tmp, imei);
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
        case LocationProvider.AVAILABLE:
            break;
        case LocationProvider.OUT_OF_SERVICE:
            currentProvider = LocationManager.NETWORK_PROVIDER;
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            if (currentProvider == LocationManager.GPS_PROVIDER) {
                currentProvider = LocationManager.NETWORK_PROVIDER;
            } else {
                currentProvider = LocationManager.GPS_PROVIDER;
            }

            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        
    }

    @Override
    public void onProviderDisabled(String provider) {
        if (provider.equals(LocationManager.GPS_PROVIDER)) {
            currentProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            currentProvider = LocationManager.GPS_PROVIDER;
        }
        Toast
            .makeText(LoctionService.this, "currentProvider:" + currentProvider, Toast.LENGTH_LONG)
            .show();
    }
}
